package al.softsynergy.auk;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

import al.softsynergy.auk.data.ejbs.IAukBean;
import al.softsynergy.auk.ui.LoginWindow;
import al.softsynergy.auk.ui.MainWindow;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

public class AukApplication extends Application{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4437920269435490244L;
	private LoginWindow loginWindow;
	private MainWindow mainWindow;
	private static final String TITLE = "Fatura Ujesjelles Kanalizime";
	
	private IAukBean aukBean;
	
	
	public AukApplication(IAukBean aukBean){
		this.aukBean = aukBean;

	}
	
	public IAukBean getAukBean() {
		return aukBean;
	}
	
	
	
	@Override
	public void init() {
		setTheme("auktheme");
		loginWindow = new LoginWindow(TITLE);
		this.loadMainWindow();
		setMainWindow(this.mainWindow);
		
	}

	
	
	public void loadMainWindow(){
		System.out.println("AukApplication.loadMainWindow()");
		if (this.mainWindow == null) {
			this.mainWindow = new MainWindow(TITLE, aukBean);
		}
		this.removeWindow(this.loginWindow);
		this.setMainWindow(this.mainWindow);
		this.loginWindow = null;
		
	}
	
	public void loadLoginWindow(){
		if (this.loginWindow == null){
			this.loginWindow = new LoginWindow(TITLE);
		}
		this.removeWindow(this.mainWindow);
		this.setMainWindow(this.loginWindow);
		this.mainWindow = null;
	}

	
	  @WebServlet(urlPatterns = "/*")
	    public static class Servlet extends AbstractApplicationServlet {

	    	@EJB
	    	private IAukBean aukBean;

	    	@Override
	    	protected Class<? extends Application> getApplicationClass() {
	    		return AukApplication.class;
	    	}

	    	@Override
	    	protected Application getNewApplication(HttpServletRequest request)
	    	throws ServletException { return new AukApplication(aukBean); }
	    }


	
	  
	  
	
}
